package com.linjiahao.userservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linjiahao.commonutils.JwtUtils;
import com.linjiahao.commonutils.MD5;
import com.linjiahao.commonutils.ResultMap;
import com.linjiahao.commonutils.online_education_Exception;
import com.linjiahao.userservice.entity.UcenterMember;
import com.linjiahao.userservice.entity.vo.LoginVo;
import com.linjiahao.userservice.entity.vo.Password;
import com.linjiahao.userservice.entity.vo.RegisterVo;
import com.linjiahao.userservice.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-06
 */
@RestController
@RequestMapping("/userservice/member")
public class UcenterMemberController {

    @Value("shipin.key")
    String key;

    @Autowired
    private UcenterMemberService memberService;

    @ApiOperation(value = "用户登录")
    @PostMapping("login")
    public ResultMap login(@RequestBody LoginVo loginVo) {
        String token = memberService.login(loginVo);
        return ResultMap.ok().data("token", token);
    }

    @ApiOperation(value = "用户注册")
    @PostMapping("register")
    public ResultMap register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return ResultMap.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getLoginInfo")
    public ResultMap getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            UcenterMember ucenterMember = memberService.getById(memberId);
            return ResultMap.ok().data("item", ucenterMember);
        }catch (Exception e){
            e.printStackTrace();
            throw new online_education_Exception(20001,"error");
        }
    }

    @GetMapping("getInfoUc/{id}")
    public UcenterMember getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = memberService.getById(id);
        return ucenterMember;
    }

    @PostMapping("auth/updateInfo")
    public ResultMap updateInfo(@RequestBody UcenterMember ucenterMember,HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        ucenterMember.setId(memberId);
        memberService.updateById(ucenterMember);
        return ResultMap.ok().message("修改成功");
    }

    @PostMapping("update/password")
    public ResultMap updatePassword(@RequestBody Password password,HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember password1 = memberService.getOne(new QueryWrapper<UcenterMember>()
                .eq("id",memberId)
                .eq("password", MD5.encrypt(password.getOldpassword())));
        if(password1==null){
            return ResultMap.error().message("原密码错误");
        }else {
            UcenterMember ucenterMember = new UcenterMember();
            ucenterMember.setId(memberId);
            ucenterMember.setPassword(MD5.encrypt(password.getNewpassword()));
            memberService.updateById(ucenterMember);
            return ResultMap.ok().message("修改成功");
        }

    }

    @GetMapping("getMenber/{input}")
    public ResultMap getMenberBy(@PathVariable String input,HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        List<UcenterMember> menber = memberService.getMenber(input,memberId);
        return ResultMap.ok().data("menber",menber);
    }


}

